# -*- coding: utf-8 -*-
'''
author:Cooper
date:2012.5.14

desc:导出TOPAPI中的表结构，作为BLL的映射表，导出为当前目录下的table_mapping.py文件
'''

import os
import time
from TopAPI.lib import getConn

template='''
# -*- coding: utf-8 -*-
\'\'\'
author:Cooper
date:2012.5.14
update:$DATE$
desc:
\'\'\'
table_mapping={
$DICT$
}
'''

cur_date=time.strftime('%Y-%m-%d %H:%M',time.localtime())

def alias(**args):
	return template.replace('$DATE$',cur_date).replace('$DICT$',args['fs'])

def main():
	tbNames=list()
	desc_list=list()
	db=getConn('TOPAPI')
	db.query('SHOW TABLES')
	r=db.store_result()
	for i in range(r.num_rows()):
		tbNames.append(r.fetch_row()[0][0])
	for tbName in tbNames:
		db.query('DESCRIBE `%s`'%tbName)
		r=db.store_result()
		t=list()
		for i in range(r.num_rows()-1):
			t.append(r.fetch_row()[0][0])
		desc_list.append("    '%s':'%s'"%(tbName,','.join(t)))
	db.close()
	with open(os.path.join(os.getcwd(),'TopAPI','table_mapping.py'),'w') as f:
		f.write(alias(fs=',\n'.join(desc_list)))

if __name__ == '__main__':
	main()